package cn.iocoder.yudao.module.ai.controller.admin.voice;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ai.controller.admin.voice.vo.AiVoiceTranscriptionReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.voice.vo.AiVoiceTranscriptionRespVO;
import cn.iocoder.yudao.module.ai.service.voice.AiVoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - AI 语音转文字
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - AI 语音转文字")
@RestController
@RequestMapping("/ai/voice")
@Validated
public class AiVoiceController {

    @Resource
    private AiVoiceService aiVoiceService;

    @PostMapping("/transcribe")
    @Operation(summary = "语音转文字", description = "将音频数据转换为文本（HTTP 接口，建议使用 WebSocket 接口以获得更好的实时性）")
    public CommonResult<AiVoiceTranscriptionRespVO> transcribe(@Valid @RequestBody AiVoiceTranscriptionReqVO reqVO) {
        try {
            String text = aiVoiceService.transcribeAudioBase64(
                    reqVO.getAudioData(),
                    reqVO.getFormat(),
                    reqVO.getLanguage()
            );
            
            AiVoiceTranscriptionRespVO respVO = AiVoiceTranscriptionRespVO.builder()
                    .text(text)
                    .finished(true)
                    .build();
            
            return success(respVO);
        } catch (Exception e) {
            AiVoiceTranscriptionRespVO respVO = AiVoiceTranscriptionRespVO.builder()
                    .error(e.getMessage())
                    .finished(true)
                    .build();
            return success(respVO);
        }
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口", description = "测试语音转文字功能是否正常")
    public CommonResult<String> test() {
        return success("AI 语音转文字服务运行正常");
    }

}





